<?php
namespace app\controller\api\v1\housing;

use app\Request;
use app\validate\api\housing\enterpriseValidate;
use think\facade\Db;

class HousingEnterprisesAuthentication
 {

	 public function enterprises_add (Request $request)
	 {
		 [$Identity_type,  $name, $phone, $Identity_id, $identity_positive, $identity_reverse, $enterprises_name, $enterprises_phone, $enterprises_address, $enterprises_license, $qualification, $door_img, $work_img]= $request->postMore([
			 ['Identity_type', ''],//认证类型
			 ['name', ''], //姓名
			 ['phone', ''], //手机号
			 ['Identity_id', ''],//身份证号码
			 ['identity_positive', ''],//身份证正面
			 ['identity_reverse', ''],//身份证反面
			 ['enterprises_name', ''],//企业名称没有不用写
			 ['enterprises_phone', ''],//企业电话
			 ['enterprises_address', ''],// 企业地址
			 ['enterprises_license', ''],// 营业执照
			 ['qualification', ''],//资质没有不用写
			 ['door_img', ''],//门头
			 ['work_img', ''],//工作区域
		 ],  true);
		 
		 $request_data = [
			 'Identity_type' => $Identity_type,
			 'name'   => $name,
			 'phone' => $phone,
			 'Identity_id' => $Identity_id,
			 'identity_positive' => $identity_positive,
			 'identity_reverse' => $identity_reverse,
			 'enterprises_name' => $enterprises_name,
			 'enterprises_phone' => $enterprises_phone,
			 'enterprises_address' => $enterprises_address,
			 'enterprises_license' => $enterprises_license,
			 'qualification' => $qualification,
			 'door_img' => $door_img,
			 'work_img' => $work_img,
		 ];
		 validate(enterpriseValidate::class)->check($request_data);
		 $data =$request_data;
		 $data['user_id']=$request->uid;
		 try {
			 $Model = new  \app\model\v1\housing\HousingEnterprisesAuthentication();
			 $person =  $Model->where ('user_id',$request->uid)->find ();
			 if (!$person){
				 $Model->insert ($data);
			 }elseif($person['status']===2){
				$ser = $Model->where ('id',$person['id'])->update($data);
				if ($ser==0){
					Db::rollback();
					return  app('json')->fail('请重新提交企业信息');

				}
				 $status['status']=0;
				 $Model->where ('id',$person['id'])->update($status);
			 }elseif($person['status']===0){
				 return app('json')->status('success', '您已申请经纪人认证，请等待');

			 }elseif($person['status']===1){
				 return app('json')->status('success', '您已认证成功，‘请勿重复提交');

			 }
		 } catch (\Exception $e) {
			 $result['msg'] = $e->getMessage();
			 $result['status'] = false;
			 return json_encode($result);
		 }

		 return app('json')->status('success', '提交成功');


	 }

	 public function get_user_enterprises (Request $request)
	 {
		 $Model = new  \app\model\v1\housing\HousingEnterprisesAuthentication();
		 $person =  $Model->where ('user_id',$request->uid)->find ();
		 if (!$person){
			 return app('json')->status('success', '没有找到您的申请记录');
		 }elseif($person['status']===2){
			 return app('json')->status('success', '您申请经纪人认证已拒绝',$person);

		 }elseif($person['status']===0){
			 return app('json')->status('success', '您已申请经纪人认证，请等待',$person);

		 }elseif($person['status']===1){
			 return app('json')->status('success', '您已认证成功，‘请勿重复提交',$person);

		 }
	 }

}
